package com.itheima.prize.api.action;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.prize.commons.db.entity.CardGame;
import com.itheima.prize.commons.db.entity.CardProductDto;
import com.itheima.prize.commons.db.entity.ViewCardUserHit;
import com.itheima.prize.commons.db.service.CardGameService;
import com.itheima.prize.commons.db.service.GameLoadService;
import com.itheima.prize.commons.db.service.ViewCardUserHitService;
import com.itheima.prize.commons.utils.ApiResult;
import com.itheima.prize.commons.utils.PageBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/api/game")
@Api(tags = {"活动模块"})
public class GameController {
    @Autowired
    private GameLoadService loadService;
    @Autowired
    private CardGameService gameService;
    @Autowired
    private ViewCardUserHitService hitService;

    @GetMapping("/list/{status}/{curpage}/{limit}")
    @ApiOperation(value = "活动列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="status",value = "活动状态（-1=全部，0=未开始，1=进行中，2=已结束）",example = "-1",required = true),
            @ApiImplicitParam(name = "curpage",value = "第几页",defaultValue = "1",dataType = "int", example = "1",required = true),
            @ApiImplicitParam(name = "limit",value = "每页条数",defaultValue = "10",dataType = "int",example = "3",required = true)
    })
    public ApiResult list(@PathVariable int status,@PathVariable int curpage,@PathVariable int limit) {
        //TODO：任务4.1-活动模块-活动列表
        QueryWrapper<CardGame> queryWrapper = new QueryWrapper<>();
        if (status == -1) {
            // 如果状态为 -1，表示查询全部活动，不需要添加额外的状态筛选条件
        } else if (status == 0) {
            queryWrapper.lt("starttime", new Date());  // 查询开始时间大于当前时间的活动（未开始）
        } else if (status == 1) {
            queryWrapper.ge("starttime", new Date()).le("endtime", new Date());  // 查询开始时间小于等于当前时间且结束时间大于等于当前时间的活动（进行中）
        } else if (status == 2) {
            queryWrapper.gt("endtime", new Date());  // 查询结束时间小于当前时间的活动（已结束）
        }

        // 创建分页对象
        Page<CardGame> page = new Page<>(curpage, limit);
        // 通过CardGameService查询符合条件的活动列表并进行分页
        Page<CardGame> resultPage = gameService.page(page, queryWrapper);

        // 将Page<CardGame>类型的数据转换为PageBean类型（假设PageBean是自定义的用于统一返回分页数据结构的类）
        Page<CardGame> all = gameService.page(new Page<>(curpage,limit),queryWrapper);
        return new ApiResult(1, "获取活动列表成功",new PageBean<CardGame>(all));
    }

    @GetMapping("/info/{gameid}")
    @ApiOperation(value = "活动信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="gameid",value = "活动id",example = "1",required = true)
    })
    public ApiResult<CardGame> info(@PathVariable int gameid) {
        //TODO：任务4.2-活动模块-活动信息
        return new ApiResult(1,"成功",gameService.getById(gameid));
    }

    @GetMapping("/products/{gameid}")
    @ApiOperation(value = "奖品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="gameid",value = "活动id",example = "1",required = true)
    })
    public ApiResult<List<CardProductDto>> products(@PathVariable int gameid) {
        //TODO：任务4.3-活动模块-奖品信息
        return new ApiResult(1,"成功",loadService.getByGameId(gameid));
    }

    @GetMapping("/hit/{gameid}/{curpage}/{limit}")
    @ApiOperation(value = "中奖列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="gameid",value = "活动id",dataType = "int",example = "1",required = true),
            @ApiImplicitParam(name = "curpage",value = "第几页",defaultValue = "1",dataType = "int", example = "1",required = true),
            @ApiImplicitParam(name = "limit",value = "每页条数",defaultValue = "10",dataType = "int",example = "3",required = true)
    })
    public ApiResult<PageBean<ViewCardUserHit>> hit(@PathVariable int gameid,@PathVariable int curpage,@PathVariable int limit) {
        //TODO：任务4.4-活动模块-中奖列表
        QueryWrapper<ViewCardUserHit> wrapper = new QueryWrapper<>();
        wrapper.eq("gameid",gameid);
        Page<ViewCardUserHit> page = hitService.page(new Page<ViewCardUserHit>(curpage,limit),wrapper);
        return new ApiResult(1, "成功",new PageBean<ViewCardUserHit>(page));
    }


}